package com.dykj.fireFighting.web.base.websocket;

import com.alibaba.fastjson.JSONObject;
import com.dykj.fireFighting.api.base.job.service.IScheduleJobService;
import com.dykj.fireFighting.api.base.system.entity.SysUser;
import com.dykj.fireFighting.api.base.system.service.ISysUserService;
import com.dykj.fireFighting.common.core.constant.WebsocketConstant;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.base.websocket.configure.SpringWebSocketConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author JacksonTu
 * @version 1.0
 * @description 公用websocket工具类
 * @since 2020/10/15 16:53
 */
@Component
@Slf4j
@ServerEndpoint(value = "/ws/common/{userId}", configurator = SpringWebSocketConfig.class) //此注解相当于设置访问URL
public class CommonWebSocket {

    private static ApplicationContext applicationContext;
    private static CopyOnWriteArraySet<CommonWebSocket> webSockets = new CopyOnWriteArraySet<>();
    private static Map<String, Session> sessionPool = new HashMap<String, Session>();
    private Session session;

    public static RedisService redisService;

    public static IScheduleJobService scheduleJobService;

    public static ISysUserService sysUserService;

    public static void setApplicationContext(ConfigurableApplicationContext applicationContext) {
        CommonWebSocket.applicationContext = applicationContext;
    }

    // 注入的时候，给类的 service 注入
    @Autowired
    public void setChatService(IScheduleJobService scheduleJobService) {
        CommonWebSocket.scheduleJobService = scheduleJobService;
    }

    // 注入的时候，给类的 service 注入
    @Autowired
    public void setSysUserService(ISysUserService sysUserService) {
        CommonWebSocket.sysUserService = sysUserService;
    }


    /**
     * 连接建立成功
     *
     * @param session
     * @param userId
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("userId") String userId) {
        try {
            this.session = session;
            webSockets.add(this);
            sessionPool.put(userId, session);
            log.info("【websocket消息】有新的连接，总数为: {}", webSockets.size());
            redisService.sSet("webSocket", userId);

            //监控中心用户
            SysUser sysUser = sysUserService.getByIds(userId);
            if (sysUser != null) {
                redisService.sSet("FireEngineJob", userId);
                int fireEngineJobSize = redisService.sGet("FireEngineJob").size();
                if (fireEngineJobSize == 1) {
                    scheduleJobService.pause(2L);
                    scheduleJobService.resume(2L);
                }
            }
        } catch (Exception e) {
            log.error("【websocket消息】连接错误:{}", e.getMessage());
        }
    }

    /**
     * 连接关闭
     */
    @OnClose
    public void onClose() {
        try {
            webSockets.remove(this);
            List<String> userId = this.session.getRequestParameterMap().get("userId");
            redisService.setRemove("webSocket", userId.get(0));
            redisService.setRemove("FireEngineJob", userId.get(0));
            int fireEngineJobSize = redisService.sGet("FireEngineJob").size();
            if (fireEngineJobSize == 0) {
                scheduleJobService.pause(2L);
            }
            log.info("【websocket消息】连接断开，总数为: {}", webSockets.size());
        } catch (Exception e) {
        }
    }

    /**
     * 收到客户端消息
     *
     * @param message
     */
    @OnMessage
    public void onMessage(String message) {
        log.info("【websocket消息】收到客户端消息: {}", message);
        JSONObject obj = new JSONObject();
        obj.put(WebsocketConstant.MSG_CMD, WebsocketConstant.CMD_CHECK);//业务类型
        obj.put(WebsocketConstant.MSG_TXT, "心跳响应");//消息内容
        session.getAsyncRemote().sendText(obj.toJSONString());
    }

    /**
     * 广播消息
     *
     * @param message
     */
    public void sendAllMessage(String message) {
        log.info("【websocket消息】广播消息: {}", message);
        for (CommonWebSocket webSocket : webSockets) {
            try {
                if (webSocket.session.isOpen()) {
                    webSocket.session.getAsyncRemote().sendText(message);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 单点消息
     *
     * @param userId
     * @param message
     */
    public void sendOneMessage(String userId, String message) {
        Session session = sessionPool.get(userId);
        if (session != null && session.isOpen()) {
            try {
                log.info("【websocket消息】广播消息: {},{}", userId, message);
                session.getAsyncRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 多人单点消息
     *
     * @param userIds
     * @param message
     */
    public void sendMoreMessage(String[] userIds, String message) {
        for (String userId : userIds) {
            Session session = sessionPool.get(userId);
            if (session != null && session.isOpen()) {
                try {
                    log.info("【websocket消息】广播消息: {}", message);
                    session.getAsyncRemote().sendText(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
